package com.miracle.test;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class 递归菜单 {
    public static void main(String[] args) throws IllegalAccessException {
        List<Menu> list = new ArrayList<>();

        list.add(new Menu(1,"系统管理",0,1,null));
        list.add(new Menu(2,"爷爷1",0,1,null));
        list.add(new Menu(3,"爷爷2",0,1,null));

        list.add(new Menu(4,"任务配置",1,2,null));
        list.add(new Menu(5,"密码策略",1,2,null));

        list.add(new Menu(6,"添加策略",5,3,null));
        list.add(new Menu(7,"删除策略",5,3,null));
        list.add(new Menu(10,"修改策略",5,3,null));
        list.add(new Menu(11,"删除策略",5,3,null));

        list.add(new Menu(8,"爸爸",2,2,null));
        list.add(new Menu(9,"自己",8,3,null));

        Map<Integer, List<Menu>> orgMenu = list.stream().collect(Collectors.groupingBy(Menu::getpId));
        List<Menu> rootMenus = orgMenu.get(0);

        List<Menu> cortMenu = new ArrayList<>();
        //processMenu(rootMenus, orgMenu,cortMenu);
        treeMenu(rootMenus, orgMenu);

        rootMenus.stream().forEach(e -> System.out.println(e));

    }


    /**
     * 多级菜单
     * @param rootMenus
     * @param orgMenu
     */
    public static void treeMenu(List<Menu> rootMenus, Map<Integer, List<Menu>> orgMenu){
        if (rootMenus == null || rootMenus.isEmpty()
                ||orgMenu == null || orgMenu.isEmpty()) throw new NullPointerException("args is blank");
        for (int i = 0; i < rootMenus.size(); i++) {
            Menu rootMenu = rootMenus.get(i);
            if(rootMenu != null){
                List<Menu> childrenMenus = orgMenu.get(rootMenu.getId());
                if (childrenMenus != null && !childrenMenus.isEmpty()){
                    rootMenu.setChildren(childrenMenus);
                    treeMenu(childrenMenus, orgMenu);
                }else{
                    rootMenu.setChildren(new ArrayList<>());
                }
            }
        }
    }

    /**
     * 同级菜单排序
     * @param rootMenus 根节点
     * @param orgMenu 根据父节点分组
     * @param cortMenu 目标数据
     * @throws IllegalAccessException
     */
    public static void processMenu(List<Menu> rootMenus, Map<Integer, List<Menu>> orgMenu, List<Menu> cortMenu) throws IllegalAccessException {
        if (rootMenus == null || rootMenus.isEmpty()
           ||orgMenu == null || orgMenu.isEmpty()) throw new IllegalAccessException("非法参数");
        for (int i = 0; i < rootMenus.size(); i++) {
            Menu rootMenu = rootMenus.get(i);
            if(rootMenu != null){
                cortMenu.add(rootMenu);
                List<Menu> childrenMenus = orgMenu.get(rootMenu.getId());
                if (childrenMenus != null && !childrenMenus.isEmpty()){
                    processMenu(childrenMenus, orgMenu, cortMenu);
                }
            }
        }
    }


}


class Menu{
    private int id;
    private String name;
    private int pId;
    private int lv;
    private List<Menu> children;

    public Menu(int id, String name, int pId, int lv, List<Menu> children) {
        this.id = id;
        this.name = name;
        this.pId = pId;
        this.lv = lv;
        this.children = children;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getpId() {
        return pId;
    }

    public void setpId(int pId) {
        this.pId = pId;
    }

    public int getLv() {
        return lv;
    }

    public void setLv(int lv) {
        this.lv = lv;
    }

    public List<Menu> getChildren() {
        return children;
    }

    public void setChildren(List<Menu> children) {
        this.children = children;
    }

    @Override
    public String toString() {
        return "Menu{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pId=" + pId +
                ", lv=" + lv +
                ", children=" + children +
                '}';
    }
}

